package com.zsl.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zsl.dao.BookDao;
import com.zsl.dao.CategoryDao;
import com.zsl.dao.impl.BookDaoImpl;
import com.zsl.dao.impl.CategoryDaoImpl;
import com.zsl.model.Book;
import com.zsl.model.Category;
import com.zsl.model.Users;
import com.zsl.service.BookService;

import java.io.Console;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookServiceImpl implements BookService {
    BookDao bookDao = new BookDaoImpl();
    CategoryDao categoryDao = new CategoryDaoImpl();

    @Override
    public Map<String, Object> getBookTableByPage(String pageNum, String bNameS, String cNameS) throws SQLException {
        List<Book> books = bookDao.getBookByPageAndSStr(pageNum, bNameS, cNameS);
        int total = 0;
        JSONArray jsonArray = new JSONArray();
        for (Book book : books) {
            Map<String, Object> map = new HashMap<>();
            map.put("b_id", book.getId());
            map.put("b_name", book.getName());
            map.put("author", book.getAuthor());
            map.put("price", book.getPrice());
            map.put("imageName", book.getImageName());
            map.put("description", book.getDescription());
            map.put("c_id", book.getCategory_id());
            map.put("c_name", book.getGenerate_category_name());
            jsonArray.add(map);
            total++;
        }
        Map<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("rows", jsonArray);
        return result;
    }

    @Override
    public boolean isNull(String[] args) {
        boolean isNull = false;
        for (String str : args) {
            if (str == null || str.equals("")) {
                isNull = true;
                break;
            }
        }
        return isNull;
    }

    @Override
    public Map<String, Object> getAllBook() throws SQLException {
        Map<String, Object> result = new HashMap<>();
        JSONArray rows = new JSONArray();
        int total = 0;

        List<Book> books = bookDao.getBook(null, null);
        if (books.isEmpty()) {
            result.put("total", 0);
            result.put("rows", rows);
            return result;
        }
        for (Book book : books) {
            Map<String, Object> book_map = new HashMap<>();
            book_map.put("b_id", book.getId());
            book_map.put("b_name", book.getName());
            book_map.put("author", book.getAuthor());
            book_map.put("price", book.getPrice());
            book_map.put("imageName", book.getImageName());
            book_map.put("description", book.getDescription());
            book_map.put("category_id", book.getCategory_id());
            book_map.put("c_name", book.getGenerate_category_name());
            rows.add(book_map);
            total++;
        }
        result.put("total", total);
        result.put("rows", rows);
        return result;
    }

    @Override
    public Map<String, Object> insertBook(Book book) throws SQLException {
        Map<String, Object> result = new HashMap<>();
        //判断目录是否存在
        if (categoryDao.getCategoryCount(
                new String[]{"category.id"},
                new String[]{book.getCategory_id()}
        ).equals("0")) {
            result.put("code",0);
            result.put("line",0);
            result.put("msg","目录不存在");
            return result;
        }
        //TODO 判断书本是否重名
        //更新书本
        int line=bookDao.insertOneBook(book);
        if(line==0){
            result.put("code",0);
            result.put("line",0);
            result.put("msg","添加书籍-失败");
            return result;
        }
        result.put("code",1);
        result.put("line",1);
        result.put("msg","添加成功");
        return result;
    }


}
